home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Misc / RZToDoList / Source / SuperText.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  4.0 KB  |  169 lines

  1. /*
  2.  * SuperText
  3.  *
  4.  *    SuperText is a subclass of the Text object that accepts files dragged
  5.  * in and displays them as icons.  Double-clicking on the file icon will
  6.  * open the file in the Workspace.  SuperText will perform properly when
  7.  * grouped in a ScrollView via IB's "group in ScrollView" function.
  8.  *
  9.  * You may freely copy, distribute and reuse the code in this example.
  10.  * This code is provided AS IS without warranty of any kind, expressed 
  11.  * or implied, as to its fitness for any particular use.
  12.  *
  13.  * Copyright 1995 Ralph Zazula (rzazula@next.com).  All Rights Reserved.
  14.  *
  15.  */
  16.  
  17. #import "SuperText.h"
  18. #import "FileImage.h"
  19. #import <appkit/appkit.h>
  20.  
  21. BOOL IncludesType(const NXAtom *types, NXAtom type)
  22. {
  23.     if (types) while (*types) if (*types++ == type) return YES;
  24.     return NO;
  25. }
  26.  
  27. @implementation SuperText
  28.  
  29. - initFrame:(const NXRect *)frameRect
  30. {
  31.     const char *fileType[] = {NXFilenamePboardType};    
  32.    NXSize aSize = {1.0E38, 1.0E38};
  33.     //
  34.     // This should be done somewhere else.  The preferred way is probably
  35.     // just to use RTFD format anyway...
  36.     //
  37.    [Text registerDirective:"FileImage" forClass:[FileImage class]];
  38.     
  39.     [super initFrame:frameRect];
  40.     [self setMonoFont:NO];
  41.     [self setOpaque:YES];
  42.    [self notifyAncestorWhenFrameChanged:YES];
  43.     [self setVertResizable:YES];
  44.     [self setHorizResizable:NO];
  45.    [self setMinSize:&(frameRect->size)];
  46.    [self setMaxSize:&aSize];
  47.    [self setAutosizing:NX_HEIGHTSIZABLE | NX_WIDTHSIZABLE];
  48.    [self setSel:0 :0];    
  49.     [self registerForDraggedTypes:fileType count:1];
  50.     return self;
  51. }
  52.  
  53. - awakeFromNib
  54. {
  55.     [self display];
  56.     return self;
  57. }
  58.  
  59. //========================================================================
  60. //
  61. // methods that implement dropping
  62. //
  63. //========================================================================
  64.  
  65. - (BOOL)readDragData:(Pasteboard *)pboard
  66. {            
  67.     return YES;
  68. }
  69.  
  70. - (NXDragOperation)draggingEntered:(id <NXDraggingInfo>)sender
  71. {
  72.     NXDragOperation sourceMask;
  73.  
  74.     sourceMask = [sender draggingSourceOperationMask];
  75.  
  76.     if (sourceMask & NX_DragOperationLink)
  77.         return NX_DragOperationLink;
  78.  
  79.     return NX_DragOperationNone;
  80. }
  81.  
  82. - (BOOL)performDragOperation:(id <NXDraggingInfo>)sender
  83. {    
  84.     return YES;
  85. }
  86.  
  87. - (BOOL)prepareForDragOperation:(id <NXDraggingInfo>)sender
  88. {
  89.     return YES;
  90. }
  91.  
  92. - concludeDragOperation:(id <NXDraggingInfo>)sender
  93. {
  94.     int length;
  95.     Pasteboard *pboard;
  96.     char *data, *file, *tab;
  97.     id newGraphic;
  98.  
  99.     pboard = [sender draggingPasteboard];
  100.  
  101.     if (IncludesType([pboard types], NXFilenamePboardType)) {
  102.         [pboard readType:NXFilenamePboardType data:&data length:&length];
  103.         file = data;
  104.         while (file) {
  105.             if (tab = strchr(file, '\t')) *tab = '\0';
  106.             newGraphic = [[FileImage alloc]
  107.                 initForImage:[[Application workspace] getIconForFile:file]
  108.                 fileName:file];
  109.                 
  110.             /* check to see if we have a selection yet, if not, make one */
  111.             if (sp0.cp < 0) {
  112.                 int textlength = [self textLength];
  113.                 [self setSel:textlength :textlength];
  114.             }
  115.             [self replaceSelWithCell:newGraphic];
  116.             file = tab ? ++tab : NULL;
  117.         }
  118.         [pboard deallocatePasteboardData:data length:length];
  119.         [[self window] setDocEdited:YES];
  120.     }
  121.     return self;
  122. }
  123.  
  124. - write:(NXTypedStream *)stream
  125. {
  126.     NXStream *theStream;
  127.     int len;
  128.     int maxLen;
  129.     char *data;
  130.     
  131.     // do the default write:
  132.     [super write:stream];
  133.     // now, write the RTFD stuff to a NXStream
  134.     theStream = NXOpenMemory(NULL,0,NX_READWRITE);
  135.     [self writeRTFDTo:theStream];
  136.     // now, get that data into a buffer
  137.     NXGetMemoryBuffer(theStream, &data, &len, &maxLen);
  138.     // and write it to the NXTypedStream that we were passed
  139.     NXWriteTypes(stream,"i",&len);
  140.    NXWriteArray(stream, "c", len, data);
  141.     NXClose(theStream);
  142.     return self;
  143. }
  144.  
  145. - read:(NXTypedStream *)stream
  146. {
  147.     NXStream *theStream;
  148.     char *data;
  149.     int count;
  150.     
  151.     [super read:stream];
  152.     NXReadTypes(stream,"i",&count);
  153.     NX_MALLOC(data,char,count);
  154.     NXReadArray(stream,"c", count, data);
  155.     theStream = NXOpenMemory(NULL,0,NX_READWRITE);
  156.     NXWrite(theStream,data,count);
  157.     [self readRTFDFrom:theStream];
  158.     NXClose(theStream);
  159.     return self;
  160. }
  161.  
  162. - (BOOL)respondsTo:(SEL)aSelector
  163. {
  164. //    printf("in SuperText respondsTo:\n");
  165.     return [super respondsTo:aSelector];
  166. }
  167.  
  168. @end
  169.